from rest_framework import serializers
from question.serializers import LabelSerializer
from user.models import User
from headlines.models import Article, Channel, Comment


# from user.serializers import UserModelSerializer
# from question.serializers import LabelModelSerializer


# 1.频道列表序列化器
class ChannelModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Channel
        fields = [
            "id",
            "name"
        ]


# 2.文章列表序列化器
class ArticleModelSerializer(serializers.ModelSerializer):
    # 嵌套序列化————
    # 需要导入user模块下的序列化器
    # user = UserModelSerializer()
    # collected_users = UserModelSerializer()

    class Meta:
        model = Article
        fields = [
            "id",
            "title",
            "content",
            "createtime",
            "user",
            "collected_users",
            "image"
        ]


# 6.1 子评论-文章序列化器
class SonCommentArticleModelSerializer(serializers.ModelSerializer):
    # 嵌套序列化——
    # 需要导入user模块下的序列化器
    # user = UserModelSerializer()
    subs = serializers.StringRelatedField()

    class Meta:
        model = Comment
        fields = "__all__"


# 6.评论文章序列化器
class CommentArticleModelSerializer(serializers.ModelSerializer):
    # 嵌套序列化——
    # 需要导入user模块下的序列化器
    # user = UserModelSerializer()
    subs = SonCommentArticleModelSerializer()

    class Meta:
        model = Comment
        fields = "__all__"

class UserModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ['username']



# 4.文章详情序列化器
class DetailOfArticleModelSerializer(serializers.ModelSerializer):
    # 嵌套序列化——
    # 需要导入user模块下的序列化器
    user = UserModelSerializer(read_only=True)
    # collected_users = UserModelSerializer()
    user_id = serializers.IntegerField()
    comments = CommentArticleModelSerializer()

    # 需要导入question模块下的序列化器
    # labels = LabelModelSerializer()

    class Meta:
        model = Article
        fields = [
            "id",
            "user",  # 外键
            "comments",  # 外键
            "title",
            "content",
            "image",
            "createtime",
            "updatetime",
            "visits",
            "thumbup",
            "comment_count",
            "channel",
            "labels",  # 外键
            "collected_users",  # 外键
            'user_id',
        ]


# 5.发布文章序列化器
class ReleaseArticleModelSerializer(serializers.ModelSerializer):
    labels = serializers.PrimaryKeyRelatedField(read_only=True, many=True)
    class Meta:
        model = Article
        fields = [
            "id",
            "content",
            "labels",
            "title",
            "channel",
            "image",
            'labels'
        ]


# 7.搜索文章序列化器
class SearchArticleModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Article
        fields = [
            'id',
            'createtime',
            'content',
            'title'
        ]
